package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.mapper.CustomerMapper;
import com.example.demo.pojo.Staff;
import com.example.demo.mapper.StaffMapper;
import com.example.demo.service.StaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.vo.ResultVO;
import com.example.demo.vo.ResultVO;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjy
 * @since 2025-05-25
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Autowired
    private StaffMapper staffMapper;

    @Override
    public ResultVO<IPage<Staff>> queryStaff(String account,String name,Integer state,Integer pageNum, Integer pageSize) throws Exception{
        pageNum = pageNum == null || pageNum < 1 ? 1 : pageNum;
        pageSize = pageSize == null || pageSize < 1 ? 10 : pageSize;
        Page<Staff> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(account)){
            queryWrapper.eq("account", account);
        }
        if(StringUtils.isNotBlank(name)){
            queryWrapper.like("name", name);
        }
        if(state!=null){
            queryWrapper.eq("state", state);
        }
        IPage<Staff> staffIPage = staffMapper.selectPage(page, queryWrapper);
        return ResultVO.success("查询成功", staffIPage);
    }

    @Override
    public ResultVO<Staff> staffLogin(Staff staff) {
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<Staff>();
        queryWrapper.eq("account", staff.getAccount());
        Staff realStaff = baseMapper.selectOne(queryWrapper);
        if(realStaff != null){
            if(realStaff.getPassword().equals(staff.getPassword())){
                if(realStaff.getState() == 1){
                    return ResultVO.success("登录成功", realStaff);
                }
                return ResultVO.fail("账号状态异常!请联系管理员!");
            }
            return ResultVO.fail("密码错误!");
        }
        return ResultVO.fail("账号不存在");
    }
    @Override
    public ResultVO<Staff> updateStaffState(Integer id,Integer state) throws Exception{
        if (state != 1 && state != 3) {
            return ResultVO.fail("无效的状态值");
        }
        UpdateWrapper<Staff> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("state", state);
        boolean updated = update(updateWrapper);
        return updated ?  ResultVO.success("状态更新成功") :
                ResultVO.fail("员工不存在或更新失败");
    }

    @Override
    public ResultVO<Staff> addStaff(Staff staff) throws Exception{
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", staff.getAccount());
        if (staffMapper.selectCount(queryWrapper) > 0) {
            return ResultVO.fail("员工账号已存在");
        }
        boolean saved = save(staff);
        return saved ?
                ResultVO.success("员工添加成功", staff) :
                ResultVO.fail("员工添加失败");
    }
}
